/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HealthMonitoringView;

import DataMining.Cluster;
import DataMining.Clustering;
import DataMining.ClusteringListener;
import Logger.ILog;
import Logger.LogManager;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.TextBox;

/**
 *
 * @author Tuan
 */
public class ClusteringForm extends AbstractForm implements ClusteringListener {
  
    private MIDletNavigation parent;
    private Vector points;
    private Vector currentPoints;
    private static final Object lockObject = new Object();
    private boolean isRunning;
    private int currentIndex;
    private Clustering clustering;
    private static ILog logger = LogManager.GetLogger(ClusteringForm.class.getName());
    private Alert alert;
    private TextBox textbox;

    public ClusteringForm(MIDletNavigation parent, Clustering clustering) {
        super("Clusering Form", null, parent);
        this.parent = parent;
        this.points = new Vector();
        this.currentPoints = new Vector();
        this.clustering = clustering;
        this.clustering.registerCallback(this);        
    }
    
    public void setClustering(Clustering clustering){
        this.clustering = clustering;
        this.clustering.registerCallback(this);
    }
    
    protected void initialize(){        
        super.initialize();
        this.alert = new Alert("Clustering", "Calculating K-means", null, null);
        Gauge gauge = new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
        this.alert.setIndicator(gauge);
        this.alert.setTimeout(Alert.FOREVER);
    }       
 
    public void start() {        
        Thread thread = new Thread(new Runnable() {

            public void run() {            
                try {
                    clustering.startMining();                
                } catch(Exception e){
                    logger.error(e.getMessage());
                }
                
            }
        });
        parent.showScreen(alert);
        thread.start();        
    }

    public void onMiningDone(Cluster[] clusters) {
        //
        logger.info("Mining is done");
        parent.showClusteringForm(false);
        this.deleteAll();
        this.append(clustering.getReport());
    }
}
